﻿Imports DataAcessLayer
Imports BusinessLogic
Imports ZakaFrontEnd
Imports System.Data
Imports System.Data.SqlClient
Imports AjaxControlToolkit

Partial Public Class Zaka
    Inherits System.Web.UI.Page

    Dim DAL As New DAL
    Dim BL As New BL

    Dim userID = DAL.ObterUserIDPorNome(User.Identity.Name).FirstOrDefault.IDUser


    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

        If Not Page.IsPostBack Then

            PreencheDados()

            If BL.VerificaDados(userID, ddlListaTrofeus.SelectedValue) Then

                btnSubmeterTrofeu.Enabled = False
                ddlTrofeu.Enabled = False

            Else

                btnSubmeterTrofeu.Enabled = True
                ddlTrofeu.Enabled = True

            End If

            If BL.VerificaDadosMVP(userID, ddlListaJogos.SelectedValue) Then

                btnSubmeter.Enabled = False
                ddlVotaMVP.Enabled = False

            Else

                btnSubmeter.Enabled = True
                ddlVotaMVP.Enabled = True

            End If

        End If

    End Sub

    Protected Function Activo(ByVal eval As String) As String

        Select Case eval
            Case ""

                Return "Votação Não Terminada"

            Case Else

                Return eval

        End Select

    End Function

#Region "Preenchimento"

    Protected Sub PreencheDados()

        'Dim ultimoJogo = DAL.ObterJogos.FirstOrDefault

        gvJogos.DataSource = DAL.ObterJogos.ToList
        gvJogos.DataBind()

        gvRank.DataSource = DAL.ObterRankSP
        gvRank.DataBind()

        gvTrofeus.DataSource = DAL.ObterTrofeus
        gvTrofeus.DataBind()

        'gvEquipaA.DataSource = DAL.ObterConstEquipasPorIDEquipaEIDJogo(ultimoJogo.IDJogos, 1)
        'gvEquipaA.DataBind()

        'gvEquipaB.DataSource = DAL.ObterConstEquipasPorIDEquipaEIDJogo(ultimoJogo.IDJogos, 2)
        'gvEquipaB.DataBind()

    End Sub

    Protected Sub ddlListaTrofeus_Init(ByVal sender As Object, ByVal e As EventArgs) Handles ddlListaTrofeus.Init

        ddlListaTrofeus.DataSource = DAL.ObterTrofeus
        ddlListaTrofeus.DataValueField = "IDTrofeu"
        ddlListaTrofeus.DataTextField = "NomeTrofeu"
        ddlListaTrofeus.DataBind()

    End Sub

    Protected Sub ddlListaJogos_Init(ByVal sender As Object, ByVal e As EventArgs) Handles ddlListaJogos.Init

        ddlListaJogos.DataSource = DAL.ObterJogos
        ddlListaJogos.DataValueField = "IDJogos"
        ddlListaJogos.DataTextField = "Data"
        ddlListaJogos.DataTextFormatString = "Data: {0:d}"
        ddlListaJogos.DataBind()

    End Sub

    Protected Sub ddlVotaMVP_Init(ByVal sender As Object, ByVal e As EventArgs) Handles ddlVotaMVP.Init

        ddlVotaMVP.DataSource = DAL.ObterListaJogadoresSP(userID, ddlListaJogos.SelectedValue)
        ddlVotaMVP.DataValueField = "IDUser"
        ddlVotaMVP.DataTextField = "NomeUtilizador"
        ddlVotaMVP.DataBind()

    End Sub

    Protected Sub ddlTrofeu_Init(ByVal sender As Object, ByVal e As EventArgs) Handles ddlTrofeu.Init

        ddlTrofeu.DataSource = DAL.ObterListaJogadoresTrofSP(userID)
        ddlTrofeu.DataValueField = "IDUser"
        ddlTrofeu.DataTextField = "NomeUtilizador"
        ddlTrofeu.DataBind()

    End Sub

#End Region

#Region "Operações"

    Protected Sub btnSubmeter_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btnSubmeter.Click

        If BL.VotarMVP(ddlVotaMVP.SelectedValue, ddlListaJogos.SelectedValue, userID) Then
            ddlVotaMVP.Enabled = False
            btnSubmeter.Enabled = False
        End If

    End Sub

    Protected Sub btnSubmeterTrofeu_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btnSubmeterTrofeu.Click

        If BL.Votar(ddlTrofeu.SelectedValue, ddlListaTrofeus.SelectedValue, userID) Then
            ddlTrofeu.Enabled = False
            btnSubmeterTrofeu.Enabled = False
        End If

    End Sub

    Protected Sub ddlListaTrofeus_SelectedIndexChanged(ByVal sender As Object, ByVal e As EventArgs) Handles ddlListaTrofeus.SelectedIndexChanged

        If Not BL.VerificaDados(userID, ddlListaTrofeus.SelectedValue) Then

            btnSubmeterTrofeu.Enabled = True
            ddlTrofeu.Enabled = True
        Else

            btnSubmeterTrofeu.Enabled = False
            ddlTrofeu.Enabled = False
        End If

    End Sub

    Protected Sub ddlListaJogos_SelectedIndexChanged(ByVal sender As Object, ByVal e As EventArgs) Handles ddlListaJogos.SelectedIndexChanged

        Dim participou = DAL.VerificaParticipacao(userID, ddlListaJogos.SelectedValue)
        Dim votou = BL.VerificaDadosMVP(userID, ddlListaJogos.SelectedValue)

        If participou = False _
        OrElse votou = True Then

            btnSubmeter.Enabled = False
            ddlVotaMVP.Enabled = False
        Else

            btnSubmeter.Enabled = True
            ddlVotaMVP.Enabled = True
        End If

        ddlVotaMVP.DataSource = DAL.ObterListaJogadoresSP(userID, ddlListaJogos.SelectedValue)
        ddlVotaMVP.DataValueField = "IDUser"
        ddlVotaMVP.DataTextField = "NomeUtilizador"
        ddlVotaMVP.DataBind()

    End Sub

    Protected Sub gvJogos_PageIndexChanging(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewPageEventArgs) Handles gvJogos.PageIndexChanging

        gvJogos.PageIndex = e.NewPageIndex

        gvJogos.DataSource = DAL.ObterJogos.ToList
        gvJogos.DataBind()

    End Sub

#End Region

#Region "Grid Pop-Up"

    <System.Web.Services.WebMethodAttribute(), System.Web.Script.Services.ScriptMethodAttribute()> _
    Public Shared Function GetDynamicContent(ByVal contextKey As String) As String
        Dim sTemp As New StringBuilder()

        Dim cnn As New SqlConnection(ConfigurationManager.ConnectionStrings("ZakaEntities").ToString())
        Dim cmd As New SqlCommand("FUT_GET_ConstEquipas", cnn)
        cmd.CommandType = CommandType.StoredProcedure

        cmd.Parameters.Add(New SqlParameter("@IDJogos", System.Data.SqlDbType.VarChar, 50, "NomeUtilizador"))
        cmd.Parameters("@IDJogos").Value = Convert.ToInt32(contextKey)

        Dim da As New SqlDataAdapter(cmd)
        Dim ds As New DataSet()
        da.Fill(ds, "teste")
        Dim total As Integer = ds.Tables("teste").Rows.Count

        If total > 0 Then
            sTemp.Append("<table>")
            sTemp.Append("<tr><td><b>Equipas:</b></td></tr>")
            For i As Integer = 0 To total - 1
                sTemp.Append(("<tr><td>" & ds.Tables("teste").Rows(i)(0).ToString() & "</td>" & "<td>") + ds.Tables("teste").Rows(i)(1).ToString() & "</td></tr>")
            Next

            sTemp.Append("</table>")
        End If


        Return (sTemp.ToString())
    End Function

    Protected Sub GridView1_RowCreated(ByVal sender As Object, ByVal e As GridViewRowEventArgs) Handles gvJogos.RowCreated
        If e.Row.RowType = DataControlRowType.DataRow Then
            ' Programmatically reference the PopupControlExtender
            Dim pce As PopupControlExtender = TryCast(e.Row.FindControl("Image1_PopupControlExtender"), PopupControlExtender)

            ' Set the BehaviorID
            Dim behaviorID As String = String.Concat("pce", e.Row.RowIndex)
            pce.BehaviorID = behaviorID

            ' Programmatically reference the Image control
            Dim i As Image = DirectCast(e.Row.Cells(1).FindControl("Image1"), Image)

            ' Add the clie nt-side attributes (onmouseover & onmouseout)
            Dim OnMouseOverScript As String = String.Format("$find('{0}').showPopup();", behaviorID)
            Dim OnMouseOutScript As String = String.Format("$find('{0}').hidePopup();", behaviorID)

            i.Attributes.Add("onmouseover", OnMouseOverScript)
            i.Attributes.Add("onmouseout", OnMouseOutScript)
        End If
    End Sub

#End Region

End Class